package ltd.hxya.novel.book.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import feign.Body;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import ltd.hxya.novel.book.dto.*;
import ltd.hxya.novel.book.entity.Book;
import ltd.hxya.novel.book.entity.BookIndex;
import ltd.hxya.novel.book.service.IBookIndexService;
import ltd.hxya.novel.book.service.IBookService;
import ltd.hxya.novel.book.vo.BookAndRankVo;
import ltd.hxya.novel.book.vo.BookIndexVo;
import ltd.hxya.novel.common.anno.AddVisitCount;
import ltd.hxya.novel.common.to.book.BookTo;
import ltd.hxya.novel.common.vo.SearchParam;
import ltd.hxya.novel.book.vo.BookVo;
import ltd.hxya.novel.common.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 * 小说表 前端控制器
 * </p>
 *
 * @author hxya
 * @since 2022-09-13
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private IBookService bookService;

    @Autowired
    private IBookIndexService bookIndexService;

    /**
     * 查询所有书籍
     */
    @GetMapping("/listAllBook")
    public Result<List<BookVo>> listAllBook(){
        List<BookVo> bookVos = bookService.listAllBook();
        return Result.success(bookVos);
    }

    /**
     * 根据不同类型获取不同的小说
     * @return
     */
    @GetMapping("/listRank")
    public Result<Page<BookVo>> listClickRank(@ApiParam("根据类型查询：1.评分查询，2性别以及分类查询，3.根据更新时间,4查询出三天上架的新书")SearchParam searchParam){
        Page<BookVo> bookVoPage = bookService.listBlank(searchParam);
        Result<Page<BookVo>> success = Result.success(bookVoPage);
        return success;
    }

    /**
     * 点击量增加
     */
    @GetMapping("/addVisitCount")
    public Result addVisitCount(Long bookId){
        bookService.addVisitCount(bookId);
        return Result.success();
    }


//    /**
//     *    * 根据搜索框中输入的值，模糊查询小说，调用search模块，从elasticsearch中查询数据，
//     *      * 并返回基本信息，通过返回的信息中的小说id，查询小说信息
//     * @param bookParam
//     * @return
//     * @throws IOException
//     */
//    @GetMapping("/listBookByCondition")
//    public Result<List<BookVo>> listBookByCondition(SearchParam bookParam) throws IOException {
//        //TODO 实现从elasticsearch中根据拼音查询
//        List<BookVo> bookVos = bookService.listBookByCondition(bookParam);
//        return Result.success(bookVos);
//    }

    /**
     *  根据排行榜分类查询小说,并将信息添加到redis中
     */
    @GetMapping("/listBookByRank/{typeId}")
    public Result<BookAndRankVo> listBookByRank(@PathVariable("typeId") Long typeId){
        BookAndRankVo bookAndRankVo = bookService.listBookByRank(typeId);
        return Result.success(bookAndRankVo);
    }

    /**
     * 根据bookId查询相关书籍的信息
     */
    @GetMapping("/bookInfoById/{bookId}")
    public Result<BookVo> bookInfoById(@PathVariable("bookId") String bookId){
        BookVo bookVo = bookService.bookInfoById(bookId);
        return Result.success(bookVo);
    }

    /**
     * 根据id的集合批量查询小说信息
     * @param batchId
     * @return
     */
    @PostMapping("/bookListByBatchId")
    public Result<List<BookTo>> bookListBatchId(@RequestBody @NotNull(message = "批量id不能为空") List<Long> batchId){
        List<BookTo> bookTos = bookService.bookListByBatchId(batchId);
        return Result.success(bookTos);
    }


    /**
     * 插入小说
     */
    @PutMapping("/saveBookInfo")
    public Result<Book> saveBookInfo(@RequestBody Book book) throws ExecutionException, InterruptedException {
        Book book1 = bookService.saveBookInfo(book);
        return Result.success(book1);
    }

    /**
     * 根据小说爬虫源和爬取的网站的小说id判断小说是否添加到数据库
     */
    @PostMapping("/isAdd")
    public Result<List<Book>> isAdd(@RequestBody Book book){
        List<Book> books = bookService.isAdd(book);
        return Result.success(books);
    }

    /**
     * 根据小说id查询小说id和小说章节
     */
    @GetMapping("/bookIndex")
    public Result<BookIndexVo> bookIndex(BookIndex bookIndex) throws ExecutionException, InterruptedException {
        //根据小说id查询小说信息和章节
        BookIndexVo bookIndexVo = bookService.bookIndex(bookIndex.getBookId());
        return Result.success(bookIndexVo);
    }


    /**
     * 小说新书榜查询接口 ok
     */
    @GetMapping("newest_rank")
    public Result<List<BookRankRespDto>> listNewestRankBooks() {
        return bookService.listNewestRankBooks();
    }

    /**
     * 小说点击榜查询接口 ok
     */
    @Operation(summary = "小说点击榜查询接口")
    @GetMapping("visit_rank")
    public Result<List<BookRankRespDto>> listVisitRankBooks() {
        return bookService.listVisitRankBooks();
    }

    /**
     * 小说更新榜查询接口 ok
     */
    @Operation(summary = "小说更新榜查询接口")
    @GetMapping("update_rank")
    public Result<List<BookRankRespDto>> listUpdateRankBooks() {
        return bookService.listUpdateRankBooks();
    }


    /**
     * 小说推荐列表查询接口,根据id查询信息
     */
    @Operation(summary = "小说推荐列表查询接口")
    @GetMapping("rec_list")
    public Result<List<BookInfoRespDto>> listRecBooks(
            @Parameter(description = "小说ID") Long bookId) throws NoSuchAlgorithmException {
        return bookService.listRecBooks(bookId);
    }

    /**
     * 小说信息查询接口
     */
    @Operation(summary = "小说信息查询接口")
    @GetMapping("{id}")
    @AddVisitCount
    public Result<BookInfoRespDto> getBookById(
            @Parameter(description = "小说 ID") @PathVariable("id") String bookId,HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        return bookService.getBookById(bookId,token);
    }

    /**
     * 小说最新章节相关信息查询接口
     */
    @Operation(summary = "小说最新章节相关信息查询接口")
    @GetMapping("last_chapter/about")
    public Result<BookChapterAboutRespDto> getLastChapterAbout(
            @Parameter(description = "小说ID") String bookId,HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        return bookService.getLastChapterAbout(bookId,token);
    }

    /**
     * 获取上一章节ID接口
     */
    @Operation(summary = "获取上一章节ID接口")
    @GetMapping("pre_chapter_id/{chapterId}")
    public Result<String> getPreChapterId(@Parameter(description = "章节ID") @PathVariable("chapterId") String chapterId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        return bookService.getPreChapterId(chapterId,token);
    }

    /**
     * 获取下一章节ID接口
     */
    @Operation(summary = "获取上一章节ID接口")
    @GetMapping("next_chapter_id/{chapterId}")
    public Result<String> getNextChapterId(
            @Parameter(description = "章节ID") @PathVariable("chapterId") String chapterId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        return bookService.getNextChapterId(chapterId,token);
    }

    /**
     * 小说内容相关信息查询接口,根据章节id，来查询章节的一些信息,这个是根据id直接查找的处理
     */
    @Operation(summary = "小说内容相关信息查询接口")
    @GetMapping("content/{chapterId}")
    public Result<BookContentAboutRespDto> getBookContentAbout(
            @Parameter(description = "章节ID") @PathVariable("chapterId") String chapterId,HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        return bookService.getBookContentAbout(chapterId,token);
    }

    /**
     * 小说搜索接口
     * 传的是一个查询条件的一个对象
     * @return 分页数据的集合
     */
    @Operation(summary = "小说搜索接口")
    @PostMapping("/books")
    public Result<Page<Book>> searchBooks(
            @RequestBody SearchParam searchParam) throws IOException {
        Page<Book> bookPage =bookService.searchBooks(searchParam);
        return  Result.success(bookPage);
    }

    /**
     * 小说分类列表查询接口
     */
    @Operation(summary = "小说分类列表查询接口")
    @GetMapping("category/list")
    public Result<List<BookCategoryRespDto>> listCategory(
            @Parameter(description = "作品方向", required = true) Integer workDirection) {
        return bookService.listCategory(workDirection);
    }
}
